package com.hyt.it.ogt.ksmonitor.statistics.mq;

import com.alibaba.fastjson.JSON;
import com.hyt.it.ogt.ksmonitor.statistics.config.StatisticsMqProperties;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
@Slf4j
@ConditionalOnClass({Channel.class, RabbitTemplate.class})
public class StatisticsProducer {
    @Resource
    private StatisticsMqProperties statisticsMqProperties;
    @Resource
    private RabbitTemplate rabbitTemplate;

    /**
     * 发布消息
     *
     * @param obj 消息体
     */
    public void sendMessage(Object obj) {
        rabbitTemplate.convertAndSend(statisticsMqProperties.getMonitorStatisticsExchange(), statisticsMqProperties.getMonitorStatisticsRoute(), JSON.toJSONString(obj));
        log.info("monitorStatisticsProducer send json:{}", JSON.toJSONString(obj));
    }
}
